package cn.kuick.match10.test2;

public class BinDecimal {

	public static final String ERROR = "Error";
	private static int MAX_LEN = 32;
	
	public String printBin(double fraction) {
		double factor = 1.0;
		StringBuilder sb = new StringBuilder("0.");
		
		for(int i=0; i<MAX_LEN; i++) {
			factor /= 2;
			
			if (fraction >= factor) {
				fraction -= factor;
				sb.append("1");
			} else {
				sb.append("0");
			}
			
			if (fraction == 0.0) {
				return sb.toString();
			}
		}
		
		return ERROR;
	}
}
